<?php

/**
* alltosun.com 服务 widget service.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2015-1-31 上午9:49:02 $
* $Id$
*/

class service_widget
{
    private $per_page = 10;
    private $page_no = 1;
    /**
     * 获取服务订单列表
     */
    public function get_service_list($params = array())
    {
        if (isset($params['per_page']) && $params['per_page']) {
            $this->per_page = $params['per_page'];
        }
        if (isset($params['page_no']) && $params['page_no']) {
            $this->page_no = $params['page_no'];
        }
        if (isset($params['refund_status']) && $params['refund_status']) {
            if ($params['refund_status'] == 5) {
                $filter['refund_status >='] = 5;
                $filter['refund_status <='] = 20;
            } elseif ($params['refund_status'] == 20) {
                $filter['refund_status >='] = 20;
                $filter['refund_status <'] = 25;
            } else {
                $filter['refund_status'] = $params['refund_status'];
            }
        }
        if (isset($params['type']) && $params['type']) {
            $filter['type'] = $params['type'];
        }
        $filter['status'] = 1;
        $filter['user_id'] = user_helper::get_user_id();

        $list = array();
        $refund_count = _model('refund')->getTotal($filter);
        if ($refund_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($refund_count)) {
                Response::assign('pager', $pager);
            }
            /* 自动查询退款单状态
            $list = _model('refund')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($this->page_no));
            foreach ($list as $v) {
                service_helper::auto_refund_status($v['id']); // 自动查询
            }
            */
            if ($pager->getPages() < $this->page_no) {
                return array();
            }

            $list = _model('refund')->getList($filter, ' ORDER BY `add_time` DESC '.$pager->getLimit($this->page_no));
            foreach ($list as $k=>$v) {
                $list[$k] = service_helper::get_refund_info($v['id'], 0);
            }
        }

        return $list;
    }

    /**
     * 获取当前订单可退款的列表
     */
    public function get_allow_relation_list($param = array())
    {
        if (isset($param['order_id']) && $param['order_id']) {
            $filter['order_id'] = $param['order_id'];
        }
        $filter['status'] = 1;

        $list = array();
        $total = _model('order_relation')->getTotal($filter);
        if ($total) {
            $list = _model('order_relation')->getList($filter);

            foreach ($list as $k => $v) {
                $refund_relation_list = _model('refund_relation')->getList(array('order_relation_id'=>$v['id'], 'order_id'=>$v['order_id']));
                if ($refund_relation_list) {
                    $refund_goods_num = 0;
                    foreach ($refund_relation_list as $key => $value) {
                        # code...
                    }
                }
            }
        }
    }

    /**
     * 自动退款状态更新
     */
    public function auto_refund_status($params = array())
    {
        $query_num = 1;  // 查询数量
        if (isset($params['query_num']) && $params['query_num']) {
            $query_num = $params['query_num'];
        }

        $filter = array(
                'type' => 1,
                'pay_type' => 3,
                'refund_pay_status' =>1
            );
        $count = _model('refund')->getTotal($filter);
        if ($count) {
            $list = _model('refund')->getList($filter, ' LIMIT 0,'.$query_num);
            $i = 0;
            $response = false;
            do {
                $pay_type = $list[$i]['pay_type'];
                $url = openapi_config::$pay_type[$pay_type]['query'];
                $response = an_curl(AnUrl($url));
                $i++;
            } while ($response);
        }
    }
}
